﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Threading;
using FinancialManagement.Models.Repository;

namespace FinancialManagement.Controllers
{
    public class ComboBoxController : Controller
    {
        ComboBoxRepository repository = new ComboBoxRepository();

        //
        // GET: /ComboBox/
        [HttpPost]
        public ActionResult LoadingRevenueTypes(string text)
        {
            Thread.Sleep(1000);
            var revenueTypes = repository.GetRevenueTypes();
            if (String.IsNullOrEmpty(text))
            {
                revenueTypes = revenueTypes.Where((p) => p.RevenueTypeName.StartsWith(text)).ToList();
            }
            return new JsonResult
            {
                Data = new SelectList(revenueTypes, "RevenueTypeCode", "RevenueTypeName")
            };
        }

        [HttpPost]
        public ActionResult LoadingChargeScopes(string text)
        {
            Thread.Sleep(1000);
            var chargeScopes = repository.GetChargeScopes();
            if (String.IsNullOrEmpty(text))
            {
                chargeScopes = chargeScopes.Where((p) => p.ChargeScopeName.StartsWith(text)).ToList();
            }
            return new JsonResult
            {
                Data = new SelectList(chargeScopes, "ChargeScopeCode", "ChargeScopeName")
            };
        }

        [HttpPost]
        public ActionResult LoadingCalculateUnits(string text)
        {
            Thread.Sleep(1000);
            var calculateUnits = repository.GetCalculateUnits();
            if (String.IsNullOrEmpty(text))
            {
                calculateUnits = calculateUnits.Where((p) => p.CalculateUnitName.StartsWith(text)).ToList();
            }
            return new JsonResult
            {
                Data = new SelectList(calculateUnits, "CalculateUnitCode", "CalculateUnitName")
            };
        }

        [HttpPost]
        public ActionResult LoadingDepartments(string text)
        {
            Thread.Sleep(1000);
            var departments = repository.GetDepartments();
            if (String.IsNullOrEmpty(text))
            {
                departments = departments.Where((p) => p.DepartmentName.StartsWith(text)).ToList();
            }
            return new JsonResult
            {
                Data = new SelectList(departments, "DepartmentID", "DepartmentName")
            };
        }

        [HttpPost]
        public ActionResult LoadingProjectTables(string text)
        {
            Thread.Sleep(1000);
            var projectTables = repository.GetProjectTables();
            if (String.IsNullOrEmpty(text))
            {
                projectTables = projectTables.Where((p) => p.ProjectName.StartsWith(text)).ToList();
            }
            return new JsonResult
            {
                Data = new SelectList(projectTables, "ProjectID", "ProjectName")
            };
        }
    }
}
